package com.ant.androiddemo;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.ant.androiddemo.constant.Global;
import com.ant.androiddemo.util.ToastUtil;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = Global.LOG_TAG;
    private static final String SPF_TAG = Global.SPF_TAG;
    private static final int REQUEST_CODE_REGISTER = 1;
    private Button btnLogin;
    private EditText etAccount, etPassword;
    private CheckBox cbRemember, cbAutoLogin;
    private String account = "";
    private String password = "";

    private ActivityResultLauncher<Intent> resultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    //判断是否传入正确的result
                    if (result.getResultCode() == RESULT_OK) {
                        Intent intent = result.getData();   //获取上一个活动返回的Intent
                        //判断上一个活动的Intent是否存在，存在则在日志中输入
                        if (intent != null) {
                            String str = intent.getStringExtra("data_return");
                            Log.d(TAG, str);
                        }
                    }
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = etAccount.getText().toString();
                String pwd = etPassword.getText().toString();

                if (TextUtils.isEmpty(account)){
                    ToastUtil.toastShort(LoginActivity.this, "请输入账号！");
                    return;
                }
                if(!TextUtils.equals(account, username)){
                    ToastUtil.toastShort(LoginActivity.this, "账号不存在！");
                    return;
                }
                if(!TextUtils.equals(password, pwd)){
                    ToastUtil.toastShort(LoginActivity.this, "密码错误！");
                    return;
                }


                SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
                SharedPreferences.Editor editor = spf.edit();
                editor.putString("account", account);
                editor.putString("password", password);
                editor.putBoolean("isRemember", false);
                editor.putBoolean("isLogin", false);

                if (cbRemember.isChecked()) {
                    editor.putBoolean("isRemember", true);
                }

                if (cbAutoLogin.isChecked()) {
                    editor.putBoolean("isLogin", true);
                }
                editor.apply();

                ToastUtil.toastShort(LoginActivity.this, "恭喜你，登录成功！");

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.putExtra("account", account);
                startActivity(intent);
                LoginActivity.this.finish();
            }
        });
        cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    cbRemember.setChecked(true);
                }
            }
        });

        cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b) {
                    cbAutoLogin.setChecked(false);
                }
            }
        });
    }

    private void initView() {
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
        cbAutoLogin = findViewById(R.id.cb_auto_login);
    }

    private void initData() {
        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        account = spf.getString("account", account);
        password = spf.getString("password", password);
        boolean isRemember = spf.getBoolean("isRemember", false);
        boolean isLogin = spf.getBoolean("isLogin", false);
        if (isLogin) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("account", account);
            startActivity(intent);
            this.finish();
        }

        if (isRemember) {
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegActivity.RESULT_CODE_REGISTER && data != null) {
            Bundle extras = data.getExtras();
            account = extras.getString("account", "");
            password = extras.getString("password", "");
            etAccount.setText(account);
            etPassword.setText(password);
        }
    }

    public void toRegister(View view) {
        Intent intent = new Intent(LoginActivity.this, RegActivity.class);
        startActivityForResult(intent, REQUEST_CODE_REGISTER);
    }

}